﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectsInfo : MonoBehaviour
{
    public static ObjectsInfo _instance;

    //public Dictionary<int, Objectinfo> object_dic = new Dictionary<int, Objectinfo>();
    public List<Objectinfo> object_dic = new List<Objectinfo>();

    public TextAsset objectstext;
    public static string Info_path = "Assets/Resources/objects.txt";

    void Awake()
    {
        ReadInfo();
        _instance = this;
        //Debug.Log("Objects Awake");
    }

    public Objectinfo getObjectinfobyid(int id){
        Objectinfo info = null;

        //object_dic.TryGetValue(id, out info);
        // 二分查找写一个
        int front = 0;
        int after = object_dic.Count - 1;
        int mid;
        while(front <= after)
        {
            mid = (front + after) / 2;
            if (object_dic[mid].id == id)
            {
                info = object_dic[mid];
                break;
            }
            else if (object_dic[mid].id > id)
            {
                after = mid - 1;
                continue;
            }
            else
            {
                front = mid + 1;
                continue;
            }
        }
        return info;
    }
    public int Check_info_is_Existed(int id)
    {
        int front = 0;
        int after = object_dic.Count - 1;
        int mid;
        while (front <= after)
        {
            mid = (front + after) / 2;
            if (object_dic[mid].id == id)
            {
                return mid;
            }
            else if (object_dic[mid].id > id)
            {
                after = mid - 1;
                continue;
            }
            else
            {
                front = mid + 1;
                continue;
            }
        }
        return -1;
    }


    public void Sort_OBJ_Dic()
    {
        object_dic.Sort(
            (left, right) =>
            {
                if (left.id > right.id) return 1;
                else if (left.id == right.id) return 0;
                else return -1;
            });
    }

    public int CreateAnother()
    {
        int obj_ID = 1001;
        while (Check_info_is_Existed(obj_ID) >= 0) obj_ID++;

        Objectinfo one_new_obj = new Objectinfo(obj_ID);
        one_new_obj.atlas_path = "Sprites/prop";
        //object_dic.Add(one_new_obj.id, one_new_obj);
        object_dic.Add(one_new_obj);

        return one_new_obj.id;
    }
    public void ReadInfo()
    {
        object_dic.Clear();

        string text = objectstext.text;
        string[] strArray = text.Split('\n');

        foreach(string str in strArray)
        {
            string[] proArray = str.Split(',');
            Objectinfo obj = new Objectinfo(int.Parse(proArray[0]));

            int id = int.Parse(proArray[0]);
            string name = proArray[1];
            string atlas_path = proArray[2];
            string icon_name = proArray[3];
            string describe = proArray[4];
            int weight = int.Parse(proArray[5]);
            string str_type = proArray[6];
            Objecttype type = Objecttype.drug;
            switch (str_type)
            {
                case "drug":
                    type = Objecttype.drug;
                    break;
                case "equipment":
                    type = Objecttype.equipment;
                    break;
                case "material":
                    type = Objecttype.material;
                    break;
                default:
                    Debug.Log("Wrong Object TYpe");
                    Debug.Log(id);
                    Debug.Log(str_type);
                    Debug.Log(str_type.Equals("material"));
                    break;
            }
            obj.getbasic(id, name, atlas_path, icon_name, describe, weight, type);
            if (type == Objecttype.drug)
            {
                int hp =  int.Parse(proArray[7]);
                int radiation = int.Parse(proArray[8]);
                int food = int.Parse(proArray[9]);
                int water = int.Parse(proArray[10]);

                int cause_buff = int.Parse(proArray[11]);
                int defuse_buff = int.Parse(proArray[12]);
                obj.get_drug(hp, radiation, food, water, cause_buff, defuse_buff);
            }
            else if (type == Objecttype.equipment)
            {
                string str_equiptype = proArray[7];
                int damage = int.Parse(proArray[8]);
                int ammo = int.Parse(proArray[9]);
                int armor = int.Parse(proArray[10]);
                int coldtime = int.Parse(proArray[11]);
                int anti_rad = int.Parse(proArray[12]);
                Equip_type equip_Type = Equip_type.Headgear;
                
                switch (str_equiptype)
                {
                    case "Headgear":
                        equip_Type = Equip_type.Headgear;
                        break;
                    case "Armor":
                        equip_Type = Equip_type.Armor;
                        break;
                    case "Hands":
                        equip_Type = Equip_type.Hands;
                        break;
                    case "Shoes":
                        equip_Type = Equip_type.Shoes;
                        break;
                    case "Weapon":
                        equip_Type = Equip_type.Weapon;
                        break;
                    case "Sub_Weapon":
                        equip_Type = Equip_type.Sub_Weapon;
                        break;
                    case "Legs":
                        equip_Type = Equip_type.Legs;
                        break;
                    default:
                        Debug.Log(str_equiptype);
                        break;
                }
                obj.get_equip(damage, ammo, armor, coldtime, anti_rad,equip_Type);
            }
            else if (type== Objecttype.material)
            {
                obj.get_mat();
            }

            object_dic.Add(obj);
            //object_dic.Add(id, obj);//添加到字典中
        }
    }
    public int WriteInfo()
    {
        string result = "";
        foreach (Objectinfo key_pair in object_dic)
        {
            string oneline = "";
            oneline += key_pair.id.ToString();
            oneline += ",";
            oneline += key_pair.name.ToString();
            oneline += ",";
            oneline += key_pair.atlas_path.ToString();
            oneline += ",";
            oneline += key_pair.icon_name.ToString();
            oneline += ",";
            oneline += key_pair.describe.ToString();
            oneline += ",";
            oneline += key_pair.weight.ToString();
            oneline += ",";
            switch (key_pair.str_type)
            {
                case Objecttype.drug:
                    {
                        oneline += "drug";
                        oneline += ",";
                        oneline += key_pair.hp;
                        oneline += ",";
                        oneline += key_pair.radiation;
                        oneline += ",";
                        oneline += key_pair.food;
                        oneline += ",";
                        oneline += key_pair.water;
                        oneline += ",";
                        oneline += key_pair.cause_buffID;
                        oneline += ",";
                        oneline += key_pair.defuse_buffID;
                        oneline += ",";
                        break;
                    }                
                case Objecttype.equipment:
                    {
                        oneline += "equipment";
                        oneline += ",";
                        switch(key_pair.equip_Type)
                        {
                            case Equip_type.Armor:
                                oneline += "Armor";
                                break;
                            case Equip_type.Hands:
                                oneline += "Hands";
                                break;
                            case Equip_type.Headgear:
                                oneline += "Headgear";
                                break;
                            case Equip_type.Legs:
                                oneline += "Legs";
                                break;
                            case Equip_type.None:
                                oneline += "None";
                                break;
                            case Equip_type.Shoes:
                                oneline += "Shoes";
                                break;
                            case Equip_type.Sub_Weapon:
                                oneline += "Sub_Weapon";
                                break;
                            case Equip_type.Weapon:
                                oneline += "Weapon";
                                break;
                            default:break;
                        }
                        oneline += ",";
                        oneline += key_pair.damage.ToString();
                        oneline += ",";
                        oneline += key_pair.ammo.ToString();
                        oneline += ",";
                        oneline += key_pair.armor.ToString();
                        oneline += ",";
                        oneline += key_pair.coldtime.ToString();
                        oneline += ",";
                        oneline += key_pair.anti_rad.ToString();
                        oneline += ",";
                        break;
                    }
                case Objecttype.material:
                    oneline += "material";
                    oneline += ",";
                    break;
                default:
                    Debug.Log("TYPE GOT ERROR");
                    return -1;
            }
            result += oneline;
            result += "\n";
        }

        result = result.Remove(result.Length - 1);
        System.IO.File.WriteAllText(Info_path, result);
        return 1;

    }
}
//id 名称  图标名称 类型（药品）	加血量 加辐射 加饱食 加饱水 止血 辐射抑制    抑制时间 止疲劳

public enum Objecttype
{
    drug,equipment,material
}

public enum Equip_type
{
    Headgear,Armor,Hands,Shoes,Legs,Weapon,Sub_Weapon,None
}
[System.Serializable]
public class Objectinfo
{
    public int id;
    public string name;//名称
    public string atlas_path;// 图片的atlas的路径地址
    public string icon_name;//图片名
    public string describe;//描述
    public Objecttype str_type;//类型
    public int weight;//重量

    public int hp;//（药品）加血量
    public int radiation;//（药品）加辐射
    public int food;//（药品）加饱食
    public int water;//（药品）加饱水


    // 这是新添加的尝试性的东西
    public int cause_buffID;// 会导致的buff的ID
    public int defuse_buffID;// 会去除的buff的ID

    public int damage;//（装备）伤害
    public int ammo;//（装备）弹夹
    public int armor;//（装备）护甲
    public float coldtime;//（装备）冷却时间
    public int anti_rad;//（装备）抗辐射
    public Equip_type equip_Type;//(装备)穿戴类型

    public Objectinfo(int default_index)
    {
        id = default_index;
        name = "default";
        icon_name = "";
        describe = "默认道具";
        str_type = Objecttype.material;
        weight = 1;
        atlas_path = "";

        hp = 0;
        radiation = 0;
        food = 0;
        water = 0;

        cause_buffID = 0;
        defuse_buffID = 0;

        damage = 0;
        ammo = 0;
        armor = 0;
        coldtime = 0;
        anti_rad = 0;
        equip_Type = Equip_type.None;

    }

    public void getbasic(int id, string name, string altas_path, string icon_name, string describe, int weight, Objecttype str_type)
    {
        this.id = id;
        this.name = name;
        this.atlas_path = altas_path;
        this.icon_name = icon_name;
        this.describe = describe;
        this.str_type = str_type;
        this.weight = weight;
        this.equip_Type = Equip_type.None;
    }

    public void get_drug(int hp, int radiation, int food, int water, int cause_buffID, int defuse_buffID)
    {
        this.hp = hp;
        this.radiation = radiation;
        this.food = food;
        this.water = water;

        this.cause_buffID = cause_buffID;
        this.defuse_buffID = defuse_buffID;
    }

    public void get_equip(int damage, int ammo, int armor, float coldtime, int anti_rad,Equip_type equip_Type)
    {
        this.damage = damage;
        this.ammo = ammo;
        this.coldtime = coldtime;
        this.anti_rad = anti_rad;
        this.equip_Type = equip_Type;
    }

    public void get_mat()
    {
        return;
    }

    public void activated()
    {

    }

    public string Get_Obj_Type()
    {
        if (str_type == Objecttype.drug) return "药品";
        else if (str_type == Objecttype.material) return "材料";
        else
        {
            if (equip_Type == Equip_type.Armor) return "装备-身甲";
            else if (equip_Type == Equip_type.Hands) return "装备-手部装甲";
            else if (equip_Type == Equip_type.Headgear) return "装备-头盔";
            else if (equip_Type == Equip_type.Legs) return "装备-腿甲";
            else if (equip_Type == Equip_type.Shoes) return "装备-鞋子";
            else if (equip_Type == Equip_type.Sub_Weapon) return "装备-副武器";
            else if (equip_Type == Equip_type.Weapon) return "装备-主武器";
            else return "装备-不明";
        }
    }
}